package com.exm.collectcodenew.java8feature.Longadder;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.LongAdder;

public class LongAdderExample {
    public static void main(String[] args) throws InterruptedException {
        // 创建LongAdder实例
        LongAdder longAdder = new LongAdder();
        int threadNumber = 50;
        CountDownLatch countDownlatch1 = new CountDownLatch(threadNumber);
        for (int i = 1; i <= threadNumber; i++) {
            new Thread(() -> {
                try {
                    for (int j = 1; j <= 100; j++) {
                        longAdder.increment();
                    }
                } finally {
                    countDownlatch1.countDown();
                }
            }, String.valueOf(i)).start();
        }
        countDownlatch1.await();
        System.out.println("longAdder.sum():"+longAdder.sum());
    }
}
